package com.jia.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.mybatis.page.PageResult;
import com.jia.product.entity.Attr;
import com.jia.product.entity.AttrGroup;
import com.jia.product.service.AttrGroupService;
import com.jia.product.vo.AttrGroupWithAttrVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 属性分组(AttrGroup)表控制层
 *
 * @author makejava
 * @since 2025-07-08 18:15:25
 */
@RestController
@RequestMapping("attr/group")
public class AttrGroupController {
    /**
     * 服务对象
     */
    @Resource
    private AttrGroupService attrGroupService;


    @GetMapping("/page/{catelogId}")
    public PageResult<AttrGroup> pageQueryCategory(
            @RequestParam(defaultValue = "0", required = false) long current,
            @RequestParam(defaultValue = "10", required = false) long size,
            @RequestParam(required = false) String key,
            @PathVariable Long catelogId) {
        Page<AttrGroup> attrGroupPage = attrGroupService.pageQuery(current, size, key, catelogId);
        return PageResult.convert(attrGroupPage);
    }


    @PostMapping("/insert")
    public AttrGroup insert(@RequestBody AttrGroup attrGroup) {
        attrGroupService.save(attrGroup);
        return attrGroup;
    }

    @PutMapping("/update")
    public AttrGroup update(@RequestBody AttrGroup attrGroup) {
        attrGroupService.updateById(attrGroup);
        return attrGroup;
    }

    @GetMapping("/info/{attrGroupId}")
    public AttrGroup getAttrGroupById(@PathVariable Long attrGroupId) {
        return attrGroupService.getByIdWithPath(attrGroupId);
    }

    /**
     * 查询属性分组下的所有属性
     *
     * @return 属性列表
     */
    @GetMapping("/list/{attrGroupId}")
    public List<Attr> getAttrGroups(@PathVariable Long attrGroupId) {
        return attrGroupService.listAttrByGroup(attrGroupId);
    }

    /**
     * 查询当前分组 未选择的属性
     *
     * @return 属性列表
     */
    @GetMapping("/page/un/{attrGroupId}")
    public Page<Attr> getUnAttrGroupsByAttrGroupId(@RequestParam(defaultValue = "0", required = false) long current,
                                                   @RequestParam(defaultValue = "10", required = false) long size,
                                                   @PathVariable Long attrGroupId) {
        return attrGroupService.unListAttrByGroup(current, size, attrGroupId);
    }

    /**
     * 查询分类下的所有分组 和 分组下的所有 属性
     *
     * @param categoryId 分类id
     * @return 分组和属性列表
     */
    @GetMapping("/category/{categoryId}")
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrVos(@PathVariable Long categoryId) {
        return attrGroupService.queryGroupWithAttrBy(categoryId);
    }

}

